package hanjia;

import java.util.Arrays;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2025/2/17-下午12:55
 * Since  :   1.0
 * Version:   1.0
 */
public class LeetCode1005 {
    public int largestSumAfterKNegations(int[] nums, int k) {
        int count=0;
        for (int i:nums){
            if (i<0){
                count++;
            }
        }
        Arrays.sort(nums);
        if (k<=count){
            int i=0;
            while (i<k){
            nums[i]=-nums[i];
                    i++;
            }

           return sum(nums);

        }else {
            int next=k-count;
            int i=0;
            while (i<count){
                nums[i]=-nums[i];
                i++;
            }
            if (next%2!=0){
                Arrays.sort(nums);
               nums[0]=-nums[0];
            }


                return sum(nums);



        }



    }

    private int sum(int[] nums) {
        int sum=0;
        for (int i:nums){
            sum+=i;
        }
        return sum;
    }


}
